package com.xuerhua.yxt.service.impl;

import java.util.Arrays;
import java.util.List;

import com.xuerhua.yxt.reqVo.TeacherIdQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.dao.TeacherInfoMapper;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.BaseQueryVO;
import com.xuerhua.yxt.reqVo.TeacherReqVO;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.view.TeacherInfoView;

@Service("teacherService")
public class TeacherServiceImpl implements ITeacherService {

	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	
	@Override
	public int delByIds(List<String> list) {
		return teacherInfoMapper.delByIds(list);
	}

	@Override
	public int insertBatch(List<TeacherInfo> list) {
		return teacherInfoMapper.insertBatch(list);
	}

	@Override
	public int insertSelective(TeacherInfo record){
		return teacherInfoMapper.insertSelective(record);
	}
	
	@Override
	public TeacherInfo selectForWapLogin(TeacherInfo record) {
		return teacherInfoMapper.selectForWapLogin(record);
	}

	@Override
	public List<TeacherInfo> selectByPage(Page<TeacherInfo> page) {
		return teacherInfoMapper.selectByPage(page);
	}

	@Override
	public TeacherInfo selectById(Integer id) {
		return teacherInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TeacherInfo> selectByParam(TeacherReqVO vo) {
		return teacherInfoMapper.selectByParam(vo);
	}

	@Override
	public int updateById(TeacherInfo record) {
		return teacherInfoMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int updateUnbindWechat(TeacherInfo record) {
		return teacherInfoMapper.updateUnbindWechat(record); 
	}

	@Override
	public TeacherInfo selectByOpenid(String openid) {
		return teacherInfoMapper.selectByOpenid(openid);
	}

	@Override
	public int selectClazzCountByOpenid(String openid) {
		return teacherInfoMapper.selectClazzCountByOpenid(openid);
	}

	@Override
	public List<TeacherInfoView> selectClazzListByOpenid(String openid) {
		return teacherInfoMapper.selectClazzListByOpenid(openid);
	}

	@Override
	public boolean isClassTeacher(Integer teacherId, Integer finishYear) {
		int count = teacherInfoMapper.isClassTeacher(teacherId, finishYear);
		return count > 0 ? true : false;
	}

	@Override
	public List<TeacherInfoView> selectClassForTeacher(BaseQueryVO vo) {
		return teacherInfoMapper.selectClassForTeacher(vo);
	}

	@Override
	public List<TeacherInfo> selectByKeyword(TeacherReqVO reqVO) {
		return teacherInfoMapper.selectByKeyword(reqVO);
	}

	@Override
	public int selectCountByOpenid(String openid) {
		return teacherInfoMapper.selectCountByOpenid(openid);
	}

	@Override
	public List<TeacherInfo> selectListForOrganizeGroup(Integer groupId) {
		return teacherInfoMapper.selectListForOrganizeGroup(groupId);
	}

	@Override
	public List<TeacherInfo> selectListByPositionIds(String postionIds) {
		List<String> array = Arrays.asList(postionIds.split(","));
		return teacherInfoMapper.selectListByPositionIds(array);
	}

	@Override
	public List<TeacherInfo> selectListByRepairType(String repairType) {
		return teacherInfoMapper.selectListByRepairType(repairType);
	}

	@Override
	public int updatePoints(Integer id, Float point) {
		int result = startUpdatePoints(id, point);
		if(result == 0) {
			for(int i = 0; i < 2; i++) {
				result = startUpdatePoints(id, point);
				if(result == 1) {
					break;
				}
			}
		}
		return result;
	}

	private int startUpdatePoints(Integer id, Float point) {
		TeacherInfo teacher = teacherInfoMapper.selectByPrimaryKey(id);
		Float points = teacher.getPoints() == null ? 0 : teacher.getPoints();
		teacher.setPoints(points + point);
		return teacherInfoMapper.updateForVersionById(teacher);
	}

	@Override
	public List<TeacherInfo> selectListForAll(TeacherReqVO vo) {
		return teacherInfoMapper.selectListForAll(vo);
	}

	/**
	 * @author:hhk
	 */
	@Override
	public List<TeacherInfo> selectListByIds(List<String> list) {
		return teacherInfoMapper.selectListByIds(list);
	}

	@Override
	public List<TeacherInfo> selectListBySchoolidAndIds(TeacherIdQueryVO vo) {
		return teacherInfoMapper.selectListBySchoolidAndIds(vo);
	}
	
	@Override
	public List<TeacherInfo> selectListByNames(List<String> list) {
		return teacherInfoMapper.selectListByNames(list);
	}

	@Override
	public List<TeacherInfo> selectAll(Integer schoolId) {
		return teacherInfoMapper.selectAll(schoolId);
	}

	@Override
	public List<TeacherInfo> selectForChatByParam(TeacherReqVO vo) {
		return teacherInfoMapper.selectForChatByParam(vo);
	}

	@Override
	public List<TeacherInfo> selectListForMainSubject(TeacherReqVO vo) {
		return teacherInfoMapper.selectListForMainSubject(vo);
	}

	@Override
	public List<TeacherInfo> selectList() {
		return teacherInfoMapper.selectList();
	}

	@Override
	public List<TeacherInfo> selectFullListByPage(Page<TeacherInfo> page) {
		return teacherInfoMapper.selectFullListByPage(page);
	}
	
}
